-
Notifications
You must be signed in to change notification settings - Fork 221
refactor(trie2): use more descriptive felt.Hash type instead of felt.Felt
#3316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #3316 +/- ##
==========================================
- Coverage 76.20% 76.18% -0.02%
==========================================
Files 346 346
Lines 32690 32773 +83
==========================================
+ Hits 24912 24969 +57
- Misses 5987 6011 +24
- Partials 1791 1793 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
9210f6f to
090e07a
Compare
ad10776 to
6bee396
Compare
6a421db to
886e905
Compare
886e905 to
96d28a1
Compare
| classNodes map[felt.Hash]map[trieutils.Path]trienode.TrieNode | ||
| contractNodes map[felt.Hash]map[trieutils.Path]trienode.TrieNode | ||
| contractStorageNodes map[felt.Hash]map[felt.Address]map[trieutils.Path]trienode.TrieNode |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
felt.Hash is the most general type, but I think it is being miss-used.
felt.Hash means that there is not a good type to define it and it makes sense to be a general type, for example the return type of hashing function.
On the other hand, here we know the semantic meaning each hash has here, so we should use the appropriate types.
As far as I understand, it should be:
classNodeskeys should be of typefelt.ClassHashcontractNodesshould be of typefelt.Addresssince it is a contract addresscontractStorageNodesshould be of typefelt.Addresstofelt.StorageAddress(orfelt.StorageSlot). <- The latter types are to be defined
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think its correct or I'm missing something here. In this particular example, The felt.Hash in the hashmap mimics the layertree, which maps state root -> trie nodeset. So the felt.Hash represents the state root. The trieutils.Path represents the class hash for class trie, address for contract trie and the storage slot for the contract storage tries
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, then I think we should have a StateRootHash
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
|
|
||
| // Child to parent layer relationship | ||
| childToParent map[felt.Felt]felt.Felt | ||
| childToParent map[felt.Hash]felt.Hash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar coment to the previous ones, what would be the right hash here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a mapping of child state root to the parent state root. Since its calculated from a few components using PoseidonHash (result of hashing function), I'd say it's correct or we introduce a special felt-like type for the state commitment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here we could use StateRootHash again
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let me add it
EgeCaner
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
core/trie2/trieutils/accessors.go
Outdated
| func ReadStateID(r db.KeyValueReader, root *felt.Felt) (uint64, error) { | ||
| key := db.StateIDKey(root) | ||
| func ReadStateID(r db.KeyValueReader, root *felt.StateRootHash) (uint64, error) { | ||
| key := db.StateIDKey((*felt.Felt)(root)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we make StateIDKey receive felt.StateRootHash?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If too big a change for this PR then @MaksymMalicki please leave a todo to update it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope, good catch, added
| func TestCacheMisses(t *testing.T) { | ||
| cache := newCleanCache(1024 * 1024) | ||
| owner := felt.FromUint64[felt.Address](1234567890) | ||
| owner := *felt.NewFromUint64[felt.Address](1234567890) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
must have slipped during rebase, changed
|
|
||
| // Different owner | ||
| diffOwner := felt.FromUint64[felt.Address](9876543210) | ||
| diffOwner := *felt.NewFromUint64[felt.Address](9876543210) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same, changed
|
|
||
| var ( | ||
| testOwner = felt.FromUint64[felt.Address](1234567890) | ||
| testOwner = *felt.NewFromUint64[felt.Address](1234567890) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mark
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed
| contractRootNode, err := trienode.DecodeNode(contractRootBlob, contractRootHash, 0, contractClassTrieHeight) | ||
| contractRootNode, err := trienode.DecodeNode( | ||
| contractRootBlob, | ||
| (*felt.Felt)(contractRootHash), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A todo for DecodeNode saying that the contractRootHash should be updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
| require.NoError(t, err) | ||
|
|
||
| owner := id.Owner() | ||
| nodeHash := node.Hash() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A todo for node.Hash() to make it's return type a felt.Hash
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
| _, found := database.dirtyCache.getNode( | ||
| &owner, | ||
| path, | ||
| (*felt.Hash)(&nodeHash), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same idea here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
| classRoot felt.Hash | ||
| contractRoot felt.Hash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What kind of hashes are these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they are dependent on the hashing function used by given trie, but those hashes are the hashed values of the children, so we either add something like felt.TrieNodeHash or we stick to the felt.Hash (which IMO is descriptive enough since the implementation of the trie is general purpose)
| err := database.GetTrieRootNodes( | ||
| (*felt.Hash)(&classRootHash), | ||
| (*felt.Hash)(&contractRootHash), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please refactor or add a todo for this function to change the parameters to the appropiate types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
| owner *felt.Address, | ||
| path *Path, | ||
| hash *felt.Felt, | ||
| hash *felt.Hash, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this Hash something more specific than felt.Hash
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope, it is the poseidon/pedersen hash of the children of this node. I think maybe some type like felt.TrieNodeHash would be okey, but generally imo felt.Hash is descriptive enough
This PR addresses #3282 (comment), and uses more descriptive
felt.Hashtype for trie node hash fields andfelt.StateRootHashfor all the state root hash fields in alltriedbandtrie2packages